<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * 公告管理控制器
 */
class Announcement extends AdminBase
{
    

    
    /**
     * 公告列表
     */
    public function announcementList()
    {
        
        $where = $this->logicAnnouncement->getWhere($this->param);
        
        $this->assign('list', $this->logicAnnouncement->getAnnouncementListForAdmin($where, true, 'sort DESC, publish_time DESC, id DESC', DB_LIST_ROWS));
        
        $this->assign('type_options', $this->logicAnnouncement->getTypeOptions());
        $this->assign('status_options', $this->logicAnnouncement->getStatusOptions());

        $announcement_status = isset($this->param['announcement_status']) ? intval($this->param['announcement_status']) : -1;
        $this->assign('announcement_status', $announcement_status);
        
        return $this->fetch('announcement_list');
    }
    
    /**
     * 公告添加
     */
    public function announcementAdd()
    {
        
        $this->announcementCommon();
        
        return $this->fetch('announcement_edit');
    }
    

    
    /**
     * 公告编辑
     */
    public function announcementEdit()
    {
        
        $this->announcementCommon();
        
        // 安全获取ID参数，避免未定义数组键错误
        $id = $this->param['id'] ?? 0;
        if ($id > 0) {
            // 通过逻辑层获取公告信息，用于后台编辑
            $info = $this->logicAnnouncement->getAnnouncementInfo(['id' => $id]);
            $this->assign('info', $info);
        }
        
        return $this->fetch('announcement_edit');
    }
    
    /**
     * 公告添加与编辑通用方法
     */
    public function announcementCommon()
    {
        
        IS_POST && $this->jump($this->logicAnnouncement->announcementEdit($this->param));
        
        $this->assign('type_options', $this->logicAnnouncement->getTypeOptions());
        $this->assign('status_options', $this->logicAnnouncement->getStatusOptions());
    }
    
    /**
     * 公告删除
     */
    public function announcementDelete()
    {
        
        $id = (int)($this->param['id'] ?? 0);
        
        $this->jump($this->logicAnnouncement->announcementDel(['id' => $id]));
    }
    
    /**
     * 公告发布
     */
    public function publishAnnouncement()
    {
        
        $id = (int)($this->param['id'] ?? 0);
        
        $this->jump($this->logicAnnouncement->publishAnnouncement(['id' => $id]));
    }
    
    /**
     * 公告下线
     */
    public function offlineAnnouncement()
    {
        
        $id = (int)($this->param['id'] ?? 0);
        
        $this->jump($this->logicAnnouncement->offlineAnnouncement(['id' => $id]));
    }
    
    /**
     * 公告排序设置
     */
    public function setSortOrder()
    {
        
        $this->jump($this->logicAnnouncement->setAnnouncementSortOrder($this->param));
    }
    
    /**
     * 公告统计
     */
    public function announcementStatistics()
    {
        
        $id = (int)($this->param['id'] ?? 0);
        
        if ($id <= 0) {
            $this->jump([RESULT_ERROR, '参数错误']);
        }
        
        $info = $this->logicAnnouncement->getAnnouncementInfo(['id' => $id]);
        if (empty($info)) {
            $this->jump([RESULT_ERROR, '公告不存在']);
        }
        
        $statistics = $this->logicAnnouncement->getAnnouncementStatistics(['announcement_id' => $id]);
        
        $this->assign('info', $info);
        $this->assign('statistics', $statistics);
        
        return $this->fetch('announcement_statistics');
    }
    
    /**
     * 导出公告列表
     */
    public function exportAnnouncementList()
    {
        
        $where = $this->logicAnnouncement->getWhere($this->param);
        
        $this->logicAnnouncement->exportAnnouncementList($where);
    }
}